package com.muyu.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.common.core.constant.UserConstants;
import com.muyu.common.core.exception.ServiceException;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.common.core.web.page.PageQueryModel;
import com.muyu.system.domain.SysPost;
import com.muyu.system.domain.model.*;
import com.muyu.system.mapper.SysPostMapper;
import com.muyu.system.service.SysPostService;
import com.muyu.system.service.SysUserPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 岗位信息 服务层处理
 *
 * @author muyu
 */
@Service
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPost> implements SysPostService {
    private final SysPostMapper postMapper;

    private final SysUserPostService sysUserPostService;

    @Autowired
    public SysPostServiceImpl(SysPostMapper postMapper, SysUserPostService sysUserPostService) {
        this.postMapper = postMapper;
        this.sysUserPostService = sysUserPostService;
    }

    /**
     * 查询所有岗位
     *
     * @return 岗位列表
     */
    @Override
    public List<SysPost> selectPostAll () {
        return this.list();
    }

    /**
     * 通过岗位ID查询岗位信息
     *
     * @param postId 岗位ID
     *
     * @return 角色对象信息
     */
    @Override
    public SysPostMyInfoModel selectPostById (Long postId) {
        return SysPostMyInfoModel.build(getById(postId));
    }

    /**
     * 根据用户ID获取岗位选择框列表
     *
     * @param userId 用户ID
     *
     * @return 选中岗位ID列表
     */
    @Override
    public List<Long> selectPostListByUserId (Long userId) {
        return postMapper.selectPostListByUserId(userId);
    }

    /**
     * 校验岗位名称是否唯一
     *
     * @param sysPostSaveModel 岗位信息
     *
     * @return 结果
     */
    @Override
    public boolean checkPostNameUnique (SysPostSaveModel sysPostSaveModel) {
        LambdaQueryWrapper<SysPost> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysPost::getPostName, sysPostSaveModel.getPostName());
        lambdaQueryWrapper.eq(sysPostSaveModel.getPostId() != null, SysPost::getPostId, sysPostSaveModel.getPostId());
        if (this.count(lambdaQueryWrapper) != 0) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
    @Override
    public boolean checkEditPostNameUnique (SysPostEditModel sysPostEditModel) {
        LambdaQueryWrapper<SysPost> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysPost::getPostName, sysPostEditModel.getPostName());
        lambdaQueryWrapper.eq(sysPostEditModel.getPostId() != null, SysPost::getPostId, sysPostEditModel.getPostId());
        if (this.count(lambdaQueryWrapper) != 0) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 校验岗位编码是否唯一
     *
     * @param sysPostSaveModel 岗位信息
     *
     * @return 结果
     */
    @Override
    public boolean checkPostCodeUnique(SysPostSaveModel sysPostSaveModel) {
        LambdaQueryWrapper<SysPost> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysPost::getPostCode, sysPostSaveModel.getPostCode());
        lambdaQueryWrapper.eq(sysPostSaveModel.getPostId() != null, SysPost::getPostId, sysPostSaveModel.getPostId());
        if (this.count(lambdaQueryWrapper) != 0) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
    @Override
    public boolean checkEditPostCodeUnique(SysPostEditModel sysPostEditModel) {
        LambdaQueryWrapper<SysPost> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysPost::getPostCode, sysPostEditModel.getPostCode());
        lambdaQueryWrapper.eq(sysPostEditModel.getPostId() != null, SysPost::getPostId, sysPostEditModel.getPostId());
        if (this.count(lambdaQueryWrapper) != 0) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 通过岗位ID查询岗位使用数量
     *
     * @param postId 岗位ID
     *
     * @return 结果
     */
    @Override
    public long countUserPostById (Long postId) {
        return sysUserPostService.countUserPostById(postId);
    }

    /**
     * 删除岗位信息
     *
     * @param postId 岗位ID
     *
     * @return 结果
     */
    @Override
    public void deletePostById (Long postId) {
       this.removeById(postId);
    }

    /**
     * 批量删除岗位信息
     *
     * @param postIds 需要删除的岗位ID
     *
     * @return 结果
     */
    @Override
    public void deletePostByIds (List<Long> postIds) {
        for (Long postId : postIds) {
            SysPostMyInfoModel post = selectPostById (postId);
            if (countUserPostById(postId) > 0) {
                throw new ServiceException(String.format("%1$s已分配,不能删除", post.getPostName()));
            }
        }
        this.removeByIds(postIds);
    }

    /**
     * 新增保存岗位信息
     *
     * @param post 岗位信息
     *
     * @return 结果
     */
    @Override
    public void insertPost (SysPost post) {
        this.save(post);
    }

    /**
     * 修改保存岗位信息
     *
     * @param post 岗位信息
     *
     * @return 结果
     */
    @Override
    public void updatePost (SysPost post) {
        this.updateById(post);
    }

    @Override
    public PageQueryModel<SysPost> pageQuery(SysPostPageQueryModel sysPostPageQueryModel) {
        LambdaQueryWrapper<SysPost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(sysPostPageQueryModel.getPostCode()),SysPost::getPostCode, sysPostPageQueryModel.getPostCode());
        queryWrapper.like(StringUtils.isNotEmpty(sysPostPageQueryModel.getPostName()),SysPost::getPostName, sysPostPageQueryModel.getPostName());
        queryWrapper.eq(StringUtils.isNotEmpty(sysPostPageQueryModel.getStatus()),SysPost::getStatus, sysPostPageQueryModel.getStatus());
        Page<SysPost> page = this.page(sysPostPageQueryModel.buildPage(), queryWrapper);

        return PageQueryModel.of(page);
    }
    @Override
    public PageQueryModel<SysPostListModel> getList(SysPostPageQueryModel sysPostPageQueryModel) {
        LambdaQueryWrapper<SysPost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(sysPostPageQueryModel.getPostCode()),SysPost::getPostCode, sysPostPageQueryModel.getPostCode());
        queryWrapper.like(StringUtils.isNotEmpty(sysPostPageQueryModel.getPostName()),SysPost::getPostName, sysPostPageQueryModel.getPostName());
        queryWrapper.eq(StringUtils.isNotEmpty(sysPostPageQueryModel.getStatus()),SysPost::getStatus, sysPostPageQueryModel.getStatus());
        Page<SysPost> page = this.page(sysPostPageQueryModel.buildPage(), queryWrapper);
        return PageQueryModel.of(page.getTotal(),page.getRecords().stream().map(SysPostListModel::listBuild).toList());
    }
}
